package com.itlong.cloud.controller.logger;

import com.itlong.cloud.POJO.DTO.operate.OperateLoggerDeveloperLogGetPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateLoggerLoginLogGetPageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateLonginLogVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.noncore.operate.IOperateLoginLogServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      运营-日志管理-登录日志管理控制器
 * </desc>
 *
 * @createDate 2018/04/10
 */
@RestController
@RequestMapping("/logger/loginLog")
public class OperateLoggerLoginLogController extends BaseController {
    @Autowired
    IOperateLoginLogServiceClient iOperateLoginLogServiceClient;


    /**
     * <desc>
     *      获取分页列表
     * </desc>
     *
     * @param dto 查询参数DTO
     * @return
     * @author Qiang.S
     * @createDate 2018/04/12
     */
    @RequestMapping(path = "getPage",method = RequestMethod.POST)
    @ResponseBody
    @AuthAccessRequired
    public Object getAppPackagePage(OperateLoggerLoginLogGetPageDTO dto){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto= HttpProtocolUtil.parseRequestParamToDTO(OperateLoggerLoginLogGetPageDTO.class,request);
            Page<OperateLonginLogVO> page=iOperateLoginLogServiceClient.getPage(dto);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);

        } catch (Exception e) {
            throw new DataAccessException("【运营_日志管理_登录日志getPage】获取分页列表失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      删除登录日志
     * </desc>
     *
     * @param ids 登录日志id数组字符串 ，号分隔
     * @return
     * @author Qiang.S
     * @createDate 2018/04/12
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.DELETE_LOGINLOG_INFO_LOG_DESC)
    @RequestMapping(path = "delete",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteLoginLog(String ids){
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            ids=request.getAttribute("ids")==null?null:request.getAttribute("ids").toString();
            iOperateLoginLogServiceClient.delete(ids);
            messageVO=new MessageVO(ids, BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch(Exception e){
            throw new DataAccessException("【运营_日志管理_登陆日志delete】删除登陆日志失败",e);
        }
        return messageVO;
    }
}
